{layout name="common/layout" /}

<div class="dislpayArrow hidden-xs"><a class="pngfix" href="javascript:void(0);" onClick="displaynavbar(this)"></a></div>
<style>
    .main_area{
        width:100%;
        height:100%;
        position: relative;
        bottom:0;
        overflow: auto;
    }
    .post_form{
        box-sizing:unset;
        padding: 15px 50px;
        margin: -10px auto;
    }
    .red_must{
        color: red;
        padding-right: 5px;
    }
    .sub_title{
        position: relative;
        float: left;
        width: 120px;
    }
    .editer{
        margin-left: 0px;
    }
    .sub_body{

    }
    /*按钮*/
    .btn_area{
        width: 45%;
        position: relative;
        margin: 40px auto;
    }
    input {
        font-size: 14px;
        color: #222;
        margin: -5px 0 0 10px;
        outline: none;
        border: none;
    }
</style>

<div class="Hui-article-box">
    <div class="Hui-article" style="top:0px;">
        <h3 class="showlist_title">活动>报名平台活动</h3>

        <div class="main_area">
            <div style="position: relative;width:100%;">
                <div class="lab cl">
                    <div class="lab-l">
                        <span class="span"><label class="red_must">&nbsp;</label>活动</span>
                    </div>
                    <div class="lab-r" style="border:0;margin-top: 8px;">
                        {$join_activity['act_name']}
                    </div>
                </div>
                <div class="lab cl">
                    <div class="lab-l">
                        <span class="span"><label class="red_must">&nbsp;</label>折扣</span>
                    </div>
                    <div class="lab-r" style="border:0;margin-top: 8px;">
                        {$join_activity['market_rate']}%
                    </div>
                </div>
                <div class="lab cl">
                    <div class="lab-l">
                        <span class="span"><label class="red_must">&nbsp;</label>报名类型</span>
                    </div>
                    <div class="lab-r" style="border:0;margin-top: 8px;">
                        {if condition="$join_activity['join_type'] eq '1'"}商品报名(每个店铺限{$join_activity['join_num']}个商品){else/}店铺报名{/if}
                    </div>
                </div>
                <div class="lab cl">
                    <div class="lab-l">
                        <span class="span"><label class="red_must">&nbsp;</label>报名店铺</span>
                    </div>
                    <div class="lab-r" style="border:0;margin-top: 8px;">
                        {$storeinfo['store_name']}
                    </div>
                </div>
                <div class="lab cl">
                    <div class="lab-l">
                        <span class="span"><label class="red_must">&nbsp;</label>店铺折扣</span>
                    </div>
                    <div class="lab-r" style="border:0;margin-top: 8px;">
                        <input name="link_url" type="text" class="input-text radius editer store_rate" style="width:100px;" onkeyup="clearNoNumAndPoint(this)" value="" />&nbsp;%&nbsp;&nbsp;
                        店铺可以自定义活动折扣(不可高于平台活动的折扣)
                    </div>
                </div>
                <div class="lab cl goods_list">
                    <div class="lab-l">
                        <span class="span"><label class="red_must">&nbsp;</label>活动商品</span>
                    </div>
                    <div class="lab-r" style="border:0;margin-top: 10px;">
                        <input type="submit" id="show-area" class="btn add_goods" style="margin-left: 0px;" value="选择商品"/>
                        <div style="margin-top: 10px;"><label>(注:同一商品只能参加一个活动，添加成功后默认取消其他活动)</label></div>
                        <div class="sel_goods_show" style="width:400px;height:200px;display: none;overflow:auto;margin-top: 20px; ">
                            <ul class="sel_goods_area">

                            </ul>
                        </div>
                    </div>
                </div>
                <div id="modal-demo"  class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content radius" style="width: 900px;">
                            <div class="modal-header">
                                <h3 class="modal-title" style="text-align: center;">选择活动商品</h3>
                                <a class="close" data-dismiss="modal" aria-hidden="true" href="javascript:void();">×</a>
                            </div>
                            <div class="modal-body">
                                <table class="goods_tab">
                                    {foreach($goodslist as $k => $v)}
                                    <tr>
                                        <td>
                                            <div>
                                                <input type="checkbox" class="goods_item" style="width:20px;height: 20px; " value="{$v['goods_id']}" goods-name="{$v['goods_name']}"  goods-image="{$v['goods_main_pic']}"/>
                                                <img src="{$v['goods_main_pic']}" style="width:60px;height:60px;"/>
                                                <label>{$v['goods_name']}</label>
                                            </div>
                                        </td>
                                    </tr>
                                    {/foreach}
                                </table>
                            </div>
                            <div class="modal-footer">
                                <span class="page_bat" style="margin-right: 20px;"></span>
                                <span class="btn previousPage">上一页</span>
                                <span class="btn nextPage">下一页</span>
                                <span class="btn select_all">全选</span>
                                <span class="btn check_btn">确定</span>
                                <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                            </div>
                        </div>
                    </div>
                </div>
                <div  class="div-box btn_area">
                    <input type="submit" class="btn add_btn" style="margin-left: 30px;" value="确认"/>
                    <input type="button" class="btn close_btn" style="margin-left: 30px;" value="取消"/>
                </div>
            </div>
        </div>

    </div>
</div>
<script type="text/javascript">
    var page = 1;
    var goods_list = new Array();
    var c_join_type = "{$join_activity['join_type']}";
    var total_page = "{$total_page}";
    $(".page_bat").html(page+"/"+total_page);
    c_join_type = parseInt(c_join_type);
    if(c_join_type == 1){
        $(".goods_list").show();
    }else{
        $(".goods_list").hide();
    }

    var c_join_num = "{$join_activity['join_num']}";
    c_join_num = parseInt(c_join_num);
    $(function(){

        //绑定活动商品相关
        $("#show-area").on('click', function(){
            goods_list.splice(0,goods_list.length);
            $("#modal-demo").modal("show");
        });
        //全选当前页面
        $(".select_all").click(function (){
            $(".goods_item").each(function(){
                $(this).prop("checked","checked");
                if($(this).prop("checked") == true){
                    var exist = $.inArray(parseInt($(this).val()),goods_list);
                    if(exist == -1){
                        goods_list.push(parseInt($(this).val()));
                    }
                }else{
                    goods_list.pop(parseInt($(this).val()));
                }
            })
        });
        //上一页
        $(".previousPage").click(function (){
            if(page == 1){
                layer.msg("已经是第一页");
                return false;
            }
            page = page-1;
            $.post("{:url('activity/addActivity')}",{page:page},function(data){
                if(data.status == 1){
                    var gdata = data.data;
                    var gtab = $(".goods_tab");
                    gtab.empty();
                    for(i=0;i<gdata.length;i++){
                        var label = $.inArray(gdata[i]['goods_id'],goods_list);
                        if(label == -1){
                            gtab.append("<tr style='margin-top:10px;'><td><div><input type='checkbox' class='goods_item' style='width:20px;height: 20px; ' value='"+gdata[i]['goods_id']+"' goods-name='"+gdata[i]['goods_name']+"'  goods-image='"+gdata[i]['goods_main_pic']+"'/>"
                                +"<img src='"+gdata[i]['goods_main_pic']+"' style='width:60px;height:60px;'/>"
                                +"<label>"+gdata[i]['goods_name']+"</label></div></td></tr>");
                        }else{
                            gtab.append("<tr style='margin-top:10px;'><td><div><input type='checkbox' class='goods_item' checked style='width:20px;height: 20px; ' value='"+gdata[i]['goods_id']+"' goods-name='"+gdata[i]['goods_name']+"'  goods-image='"+gdata[i]['goods_main_pic']+"'/>"
                                +"<img src='"+gdata[i]['goods_main_pic']+"' style='width:60px;height:60px;'/>"
                                +"<label>"+gdata[i]['goods_name']+"</label></div></td></tr>");
                        }
                    }
                }
            },'json');
            $(".page_bat").html(page+"/"+total_page);
        });
        //下一页
        $(".nextPage").click(function (){
            page = page+1;
            $.post("{:url('activity/addActivity')}",{page:page},function(data){
                if(data.status == 1){
                    var gdata = data.data;
                    var gtab = $(".goods_tab");
                    gtab.empty();
                    for(i=0;i<gdata.length;i++){
                        var label = $.inArray(gdata[i]['goods_id'],goods_list);
                        if(label == -1){
                            gtab.append("<tr style='margin-top:10px;'><td><div><input type='checkbox' class='goods_item' style='width:20px;height: 20px; ' value='"+gdata[i]['goods_id']+"' goods-name='"+gdata[i]['goods_name']+"'  goods-image='"+gdata[i]['goods_main_pic']+"'/>"
                                +"<img src='"+gdata[i]['goods_main_pic']+"' style='width:60px;height:60px;'/>"
                                +"<label>"+gdata[i]['goods_name']+"</label></div></td></tr>");
                        }else{
                            gtab.append("<tr style='margin-top:10px;'><td><div><input type='checkbox' class='goods_item' checked style='width:20px;height: 20px; ' value='"+gdata[i]['goods_id']+"' goods-name='"+gdata[i]['goods_name']+"'  goods-image='"+gdata[i]['goods_main_pic']+"'/>"
                                +"<img src='"+gdata[i]['goods_main_pic']+"' style='width:60px;height:60px;'/>"
                                +"<label>"+gdata[i]['goods_name']+"</label></div></td></tr>");
                        }

                    }
                    $(".page_bat").html(page+"/"+total_page);
                }else{
                    page = page-1;
                    $(".page_bat").html(page+"/"+total_page);
                }
            },'json');
        });

        $("body").on("click","input[type='checkbox'].goods_item",function(){
            if($(this).prop("checked") == true){
                goods_list.push(parseInt($(this).val()));
            }else{
                goods_list.pop(parseInt($(this).val()));
            }
        });

        $(".check_btn").click(function (){
            if(goods_list.length < c_join_num){
                layer.msg("商品个数不能少于最少报名个数");
                return false;
            }
            $(".sel_goods_area").empty();
            if(goods_list.length > 0){
                $(".sel_goods_show").show();
            }else{
                $(".sel_goods_show").hide();
            }
            if(goods_list.length == 0){
                $("#modal-demo").modal("hide");
                return false;
            }
            var pdata = goods_list.join(",");
            $.post("{:url('activity/getSelectGoods')}",{data:pdata},function(data){
                if(data.status == 1){
                    var agdata = data.data;
                    for(i=0;i<agdata.length;i++){
                        $(".sel_goods_area").append(
                            "<li><img src='"+agdata[i]['goods_main_pic']+"' style='width:40px;height:40px;margin-right: 10px;'/ >"+agdata[i]['goods_name']+"</li>"
                        );
                    }
                }
            },'json');
            //goods_list.splice(0,goods_list.length);
            $("#modal-demo").modal("hide");
        });

    });

    /**
     * 数据规范化函数只能是数字
     */
    function clearNoNumAndPoint(obj){
        obj.value = obj.value.replace(/[^\d]/g,"");  //清除“数字”和“.”以外的字符
        obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
        if(obj.value.indexOf(".")< 0 && obj.value !=""){
            //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
            obj.value= parseFloat(obj.value);
        }
    }

    /**
     * 数据提交
     */
    $(".add_btn").click(function(){
        var store_rate = $(".store_rate").val();
        var platom_rate = "{$join_activity['market_rate']}";
        if(parseInt(store_rate) > parseInt(platom_rate)){
            layer.msg("店铺折扣不能大于平台活动的折扣");
            return false;
        }
        var data = {
            id : "{$join_activity['id']}",
            type : c_join_type,
        };
        if(goods_list.length>0){
            data.goods_list=goods_list;
        }else{
            if(c_join_type == 1){
                layer.msg("请添加商品");
                return false;
            }
            data.goods_list=0;
        }
        if(store_rate == ""){
            data.store_rate = 0;
        }else{
            data.store_rate = parseInt(store_rate);
        }

        $.post("{:url('activity/joinActivity')}",{data:data},function(data){
            if(data.status == 1){
                layer.msg(data.msg);
                window.location.href ="{:url('activity/listPingTaiActivity')}?page={$page}";
            }else{
                layer.msg(data.msg);
            }
        },'json');
    });

    $(".close_btn").click(function(){
        window.location.href ="{:url('activity/listPingTaiActivity')}?page={$page}";
    });
</script>